/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * addRobot.java
 *
 * Created on 03/05/2010, 01:39:47 PM
 */

package UI;
import Excepciones.BasesdeDatos;
import Excepciones.ProcesamientoDato;
import Excepciones.RobotException;
import Excepciones.Sistema;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import robot_maker.*;


/**
 *
 * @author Miguel
 */
public class addRobot extends javax.swing.JFrame {
    
    /** Creates new form addRobot */
    public addRobot() {
        initComponents();
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
    }
    private UI_Robots owner;
    private boolean modify;
    private int idRobot;
    private String rName;
    addRobot(UI_Robots aThis, boolean mod, int id) {
        modify = mod;
        owner = aThis;
        idRobot = id;
        initComponents();
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);

        if(mod){
            Robot datos = new Robot();
            try {
                try {
                    datos = datos.getDatos(id);
                } catch (ProcesamientoDato ex) {
                }
            } catch (Exception ex) {
         Sistema.loggear(11, "Contructor",
                        "EN EL SQL DE EJECUCION",
                        addRobot.class, "SQLException");
            }
            nombre_robot.setText(datos.get_nombre());
            rName = datos.get_nombre();
            peso_robot.setText(String.valueOf(datos.getPeso()));
            tamano_robot.setText(String.valueOf(datos.getTamano()));
            desc_robot.setText(datos.getDescripcion());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        ciRif_label = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        nombre_robot = new javax.swing.JTextField();
        bot_cancelarGuardarRobot = new javax.swing.JButton();
        bot_guardarRobot = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        desc_robot = new javax.swing.JTextArea();
        peso_robot = new javax.swing.JFormattedTextField();
        tamano_robot = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Robots");

        jLabel1.setText("Modelo de Robot:");

        ciRif_label.setText("Nombre:");

        jLabel2.setText("Peso:");

        jLabel3.setText("Tamano:");

        jLabel4.setText("Descripción:");

        bot_cancelarGuardarRobot.setText("Cancelar");
        bot_cancelarGuardarRobot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bot_cancelarGuardarRobotActionPerformed(evt);
            }
        });

        bot_guardarRobot.setText("Guardar");
        bot_guardarRobot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bot_guardarRobotActionPerformed(evt);
            }
        });

        desc_robot.setColumns(20);
        desc_robot.setRows(5);
        jScrollPane1.setViewportView(desc_robot);

        peso_robot.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));

        tamano_robot.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ciRif_label)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(tamano_robot, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(peso_robot, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nombre_robot, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(bot_cancelarGuardarRobot)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(bot_guardarRobot))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ciRif_label)
                    .addComponent(nombre_robot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(peso_robot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tamano_robot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bot_guardarRobot)
                    .addComponent(bot_cancelarGuardarRobot))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bot_cancelarGuardarRobotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bot_cancelarGuardarRobotActionPerformed
    this.setVisible(false);
        try {
            owner.refrescar();
        } catch (BasesdeDatos ex) {
            Sistema.loggear(11, "Cancelar",
                        "EN EL SQL DE EJECUCION",
                        addRobot.class, "SQLException");
        }
}//GEN-LAST:event_bot_cancelarGuardarRobotActionPerformed

    private void bot_guardarRobotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bot_guardarRobotActionPerformed
        try {
            Robot robot = null;
            robot = new Robot(nombre_robot.getText(),
                    peso_robot.getText(), tamano_robot.getText(),
                    desc_robot.getText());
            if (modify) {
                robot.setCodigo(idRobot);
                robot.modificarBD(robot, rName);
            } else {
                try {
                    robot.ingresarBD(robot);
                } catch (Exception ex) {

            Sistema.loggear(11, "Guardar",
                        "EN EL SQL DE EJECUCION",
                        addRobot.class, "SQLException");

        }
            }
            this.setVisible(false);

        } catch (ProcesamientoDato ex) {
        } catch (RobotException ex) {
        }
 try {
                owner.refrescar();
            } catch (BasesdeDatos ex) {
            }
    
    
}//GEN-LAST:event_bot_guardarRobotActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new addRobot().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bot_cancelarGuardarRobot;
    private javax.swing.JButton bot_guardarRobot;
    private javax.swing.JLabel ciRif_label;
    private javax.swing.JTextArea desc_robot;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nombre_robot;
    private javax.swing.JFormattedTextField peso_robot;
    private javax.swing.JFormattedTextField tamano_robot;
    // End of variables declaration//GEN-END:variables

}
